package com.gxc.blog.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.gxc.authcore.system.domain.User;
import com.gxc.authcore.system.util.CurrentUtil;
import com.gxc.blogm.domain.Article;
import com.gxc.blogm.domain.Comment;
import com.gxc.blogm.service.ArticleService;
import com.gxc.blogm.service.CommentService;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.RequestUtil;
import com.gxc.webcore.util.StringUtil;

public class ArticleController {

	ArticleService articleService;
	CommentService commentService;

	public CommentService getCommentService() {
		return commentService;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	public ArticleService getArticleService() {
		return articleService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	/**
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("blog/article/article_list.vm");
	}

	/**
	 * 获取文章列表json
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView grid(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		Map params = RequestUtil.getInMap(request);
		PageList pageList = articleService.listPage(params, pageInfo);
		Map model = new HashMap();
		model.put("status", "success");
		model.put("data", pageList);
		String jsonResult = JsonUtil.object2json(model);
		response.getWriter().write(jsonResult);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String aritcle_id = request.getParameter("id");
		articleService.updateViewNum(aritcle_id);
		Article article = articleService.retrieve(aritcle_id);
		Map model = new HashMap();
		model.put("article", article);

		// 上一篇文章
		List list = null;
		HashMap params = new HashMap();
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageSize(1);
		pageInfo.setPageCurrent(1);
		params.clear();
		params.put("fieldSort", "create_time desc");
		params.put("create_time_end_no", article.getCreate_time());
		list = articleService.listPage(params, pageInfo).getList();
		if (list != null && list.size() > 0) {
			model.put("pre_article", list.get(0));
		}

		// 下一篇文章
		pageInfo.setPageSize(1);
		pageInfo.setPageCurrent(1);
		params.clear();
		params.put("fieldSort", "create_time asc");
		params.put("create_time_start_no", article.getCreate_time());
		list = articleService.listPage(params, pageInfo).getList();
		if (list != null && list.size() > 0) {
			model.put("next_article", list.get(0));
		}

		return new ModelAndView("blog/article/article_view.vm", model);
	}

	/**
	 * 创建评论保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView createCommentSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = new HashMap();
		Comment comment = (Comment) RequestUtil.getInObject(request,
				Comment.class);
		if (comment.getContent() == null
				|| comment.getContent().trim().equals("")) {
			map.put("status", "failed");
			map.put("data", "评论内容不能为空。");
		} else {
			comment.setId("comment_" + StringUtil.getUUID());
			comment.setRelate_type(1);
			User user = CurrentUtil.getAuthUser();
			if (user != null) {
				comment.setCreate_user(user.getId());
			}
			comment.setCreate_time(new Date());
			comment.setCreate_ip(request.getRemoteAddr());
			comment.setParent_id(null);
			commentService.create(comment);
			// 更新评论次数
			articleService.updateCommentNum(comment.getRelate_id());
			map.put("status", "success");
		}
		String result = JsonUtil.object2json(map);
		response.getWriter().write(result);
		return null;
	}

	/**
	 * 获取文章评论json
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView listComments(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String relate_id = request.getParameter("relate_id");
		String fieldSort = request.getParameter("fieldSort");
		Map result = new HashMap();
		if (relate_id == null || relate_id.trim().equals("")) {
			result.put("status", "failed");
			result.put("data", "relate_type和relate_id不能为空。");
		} else {
			Map params = new HashMap();
			params.put("relate_type", "1");
			params.put("relate_id", relate_id);
			params.put("fieldSort", fieldSort);
			List list = commentService.listAll(params);
			result.put("status", "success");
			result.put("data", list);
		}
		String jsonpResult = JsonUtil.object2json(result);
		response.getWriter().write(jsonpResult);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView search(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String key_word = request.getParameter("key_word");
		Map model = new HashMap();
		if (key_word != null && !key_word.trim().equals("")) {
			model.put("key_word", key_word.trim());
		}
		String year_month = request.getParameter("year_month");
		if (year_month != null && !year_month.trim().equals("")) {
			model.put("year_month", year_month.trim());
		}
		String js_obj = JsonUtil.object2json(model);
		model.put("js_obj", js_obj);
		return new ModelAndView("blog/article/article_search.vm", model);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView searchQuery(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		Map params = new HashMap();
		// 关键字
		String key_word = request.getParameter("key_word");
		if (key_word != null && !key_word.trim().equals("")) {
			params.put("key_word", key_word);
		}
		// 日期
		String year_month = request.getParameter("year_month");
		if (year_month != null && !year_month.trim().equals("")) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date create_time_start = sdf.parse(year_month);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(create_time_start);
			calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
			Date create_time_end = calendar.getTime();
			params.put("create_time_start", create_time_start);
			params.put("create_time_end", create_time_end);
		}
		PageList pageList = articleService.search(params, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String jsonResult = JsonUtil.object2json(result);
		response.getWriter().write(jsonResult);
		return null;
	}

}
